package ru.ipccenter.jsf;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import jcifs.smb.SmbException;
import org.apache.log4j.PropertyConfigurator;
import ru.ipccenter.main.MainClass;

/**
 *
 * @author Krems
 */
@ManagedBean
@SessionScoped
public class MainBean {
//url    
    private String url = "";

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrl() {
        return url;
    }
//regexp
    private String regexp = "";

    public void setRegexp(String regexp) {
        this.regexp = regexp;
    }

    public String getRegexp() {
        return regexp;
    }
//serverOut    
    private String serverOut = "smb://";
    
    public void setServerOut(String serverOut) {
        this.serverOut = serverOut;
    }

    public String getServerOut() {
        if(url.equals("")){
            return "";
        }
        return serverOut + url;
    }
//list
    private ArrayList<String> list = new ArrayList<String>();

    public void setList(ArrayList<String> list) {
        this.list = list;
    }

    public ArrayList<String> getList() {
        return list;
    }
//find
    private ArrayList<String> findBox = new ArrayList<String>();

    public void setFindBox(ArrayList<String> findBox) {
        this.findBox = findBox;
    }

    public ArrayList<String> getFindBox() {
        return findBox;
    }
//outMessage
    private String outMessage = "";
    
    public void setOutMessage(String outMessage) {
        this.outMessage = outMessage;
    }

    public String getOutMessage() {
        return outMessage;
    } 
    
//button list
    private MainClass connection = null;
    public void onListButtonClick(){
        this.list.clear();
        this.setOutMessage("");
        connection=null;
        connection = new MainClass();
        try{
            connection.newConnection(url);
            if(connection!=null)
                this.list.addAll(connection.listDir());
            else
                this.setOutMessage("Wrong operation!");
        } catch (Exception e){
            this.setOutMessage(e.getMessage());
            connection.close();
            connection=null;
        }
    }
 //button find
    public void onFindButtonClick(){
        System.out.println("It is me!");
        if(connection!=null){
            this.findBox.clear();
            try {
                this.setFindBox(connection.find(this.getRegexp(), url));
            } catch (MalformedURLException ex) {
                Logger.getLogger(MainBean.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SmbException ex) {
                Logger.getLogger(MainBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            this.setOutMessage("No connection established yet");
        }
    }
    public MainBean() {
    }
}
